<template>
  <XtxDialog :title="`${formData.id ? '修改':'添加'}收货地址`" v-model:visible="dialogVisible">
    <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input
            class="input"
            placeholder="请输入收货人"
            v-model="formData.receiver"
          />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input
            class="input"
            v-model="formData.contact"
            placeholder="请输入手机号"
          />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity
            :fullLocation="formData.fullLocation"
            placeholder="请选择所在地区"
            @change="CityCom"
          />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input
            class="input"
            v-model="formData.provinceCode"
            placeholder="请输入详细地址"
          />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input
            class="input"
            v-model="formData.address"
            placeholder="请输入邮政编码"
          />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input
            class="input"
            v-model="formData.addressTags"
            placeholder="请输入地址标签，逗号分隔"
          />
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton
        type="gray"
        style="margin-right: 20px"
        @click="dialogVisible = false"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { reactive, ref } from 'vue'
import { AddAddress, EditAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
    name:'AddressEdit',
    setup(props,{emit}){
        const dialogVisible = ref(false)
        const open = (address) => {
            dialogVisible.value = true
            if(address.id){
              // 修改--填充文本框数据
              for(let key in address){
                formData[key] = address[key]
                // console.log(formData,'9999999999999')
              }
            }else{
              // 添加
            for (const key in formData) {
              if(key === 'isDefault'){
                formData[key] = 1//??
              }else{
                formData[key] = null
              }
            }
          
            }
        }
        const formData = reactive({
            receiver: null,
            contact: null,
            provinceCode: '',
            cityCode: null,
            countyCode: null,
            address: null,
            postalCode: null,
            addressTags: null,
            isDefault: 0,
            fullLocation: null
        })

        const CityCom = (result) => {
            formData.provinceCode = result.provinceCode
            formData.cityCode = result.cityCode
            formData.countyCode = result.countyCode
            formData.fullLocation = result.fullLocation
        }
        const submit = () => {
          // 修改
          if(formData.id){
            EditAddress(formData).then(data=>{
              Message({type:"success",text:"修改成功"})
              dialogVisible.value = false
              emit('on-success', formData)
            })
          }else{
            // 添加
            AddAddress(formData).then(data=>{
                formData.id = data.result.id
                Message({type:"success",text:"添加成功"})
                dialogVisible.value = false
                emit('on-success', formData)
            })
          }
        }
        return { dialogVisible, open, CityCom, formData, submit }
    }
}
</script>

<style scoped lang="less">
.xtx-form {
  :deep(.wrapper) {
    // width: 780px;
    width: 700px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 30px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
